package api

import (
	"encoding/json"
	"net/http"
	"time"
)

const (
	RET_JSON_OK = iota
	RET_JSON_FAIL
)

func Start(addr string) {
	mux := http.NewServeMux()
	mux.HandleFunc("/time", GetServerTime)
	mux.HandleFunc("/newgame", NewGame)
	mux.HandleFunc("/allgame", AllGame)
	go http.ListenAndServe(addr, mux)
}

func GetServerTime(w http.ResponseWriter, req *http.Request) {
	w.Write([]byte(time.Now().Format("2006-01-02 15:04:05.999 -0700 MST")))
}

func retJson(w http.ResponseWriter, body interface{}) {
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	json.NewEncoder(w).Encode(body)
}
